<?php
namespace app\admin\model;
use think\Model;
use clt\Ucenter;
class Users extends Model
{
	public function login($data){
        // if(is_email($data['email'])){
        //     $map['email'] = $data['email'];
        // }else{
        //     $map['username'] = $data['email'];
        // }
        $map['email'] = $data['email'];
		$user=$this->where($map)->field('email,is_open,admin_id,group_id,sup_id,pwd as password,telephone,tel as mobile,company,address,position')->find();
		if($user){
			if($user['is_open'] != 1){
				return -2; //密码错误
			}
			if($user['password'] == md5($data['password'])){
				session('email',$user['email']);
				session('aid',$user['admin_id']);
                session('groupId',$user['group_id']);
				$avatar = '/static/admin/images/0.jpg';
				session('avatar',$avatar);
                $Ucenter = new Ucenter;
                $reu = $Ucenter->syncUser($user->toArray());
				return 1; //信息正确
			}
		}

        if(!is_email($data['email'])){
            return -1;
        }
        $Ucenter = new Ucenter;
        $result = $Ucenter->checkLogin($data['email'],md5($data['password']));
        if(false !== $result){
            $result['tel'] = $result['mobile'];
            $result['pwd'] = $result['password'];
            if($user){
                $user->tel = $result['mobile'];
                $user->pwd = $result['password'];
                $user->is_open = 1;
                $user->save();
            }else{
                $result['group_id'] = intval($data['group_id']) > 0 ? $data['group_id'] : 10000;
                $result['is_open'] = intval($data['is_open']) > 0 ? $data['is_open'] : 1;
                $this->save($result);
                $user = $this;
            }
            session('email',$user->email);
            session('aid',$user->admin_id);
            session('groupId',$user->group_id);
            $avatar = '/static/admin/images/0.jpg';
            session('avatar',$avatar);
            return 1; //信息正确
            
        }

        return -1; //用户不存在
	}
    /*获取用户归属*/
    public static function getUserInfo($user_id){
        if(empty($user_id)){
            return null;
        }
        return db('Users')->alias('u')
                ->join('auth_group ag','u.group_id = ag.group_id','left')
                ->field('u.*,ag.title')
                ->where('admin_id',$user_id)->find();
    }
	public static function getUsername($user_id){
		return $username = db('Users')->where('admin_id',$user_id)->value('username');
	}
    public static function getUserId($email){
        return db('Users')->where('email',$email)->value('admin_id');
    }
    public static function getEmail($user_id){
        return db('Users')->where('admin_id',$user_id)->value('email');
    }
	/*判断是否为客户*/
    public static function isCustomer($user_id){
    	$group_id = db('Users')->where('admin_id',$user_id)->value('group_id');
    	return $group_id==config('customer_id') ? true : false;
    }
    /*判断是否为管理员*/
    public static function isAdmin($user_id){
    	$group_id = db('Users')->where('admin_id',$user_id)->value('group_id');
    	return $group_id==1 ? true : false;
    }
    /*判断是否为客户服务*/
    public static function isService($user_id){
    	$group_id = db('Users')->where('admin_id',$user_id)->value('group_id');
    	return $group_id==4 ? true : false;
    }
    /*判断是否为客户服务*/
    public static function isChannel($user_id){
        $group_id = db('Users')->where('admin_id',$user_id)->value('group_id');
        return $group_id==3 ? true : false;
    }
    /*业务员*/
    public static function isSalesman($user_id){
        $group_id = db('Users')->where('admin_id',$user_id)->value('group_id');
        return $group_id==2 ? true : false;
    }
    /*获取上级的id*/
    public static function getSupId($user_id){
 	   $supId = db('Users')->where('admin_id',$user_id)->value('sup_id');
 	   return (int)$supId;
    }
    /*是否为工程师*/
    public static function isEngineer($user_id){
        $group_id = db('Users')->where('admin_id',$user_id)->value('group_id');
        return $group_id==5 ? true : false;
    }
    /*获取工程师*/
    public static function getEngineers(){
        return db('Users')->where('group_id',5)->select();
    }
    /*业务员*/
    public static function getCustomerList($user_id){
    	$lists = db('Users')->where('sup_id',$user_id)->field('admin_id')->select();
    	$ids = [];
    	if(is_array($lists) && $lists){
    		foreach($lists as $vo){
    			$ids[] = $vo['admin_id'];
    		}
    	}
    	return $ids;
    }
    /*找出所有下级的id*/
    public static function getCustomerSups($user_id,&$userIds){
        $lists = db('Users')->where('sup_id',$user_id)->column('admin_id,email,sup_id,group_id','admin_id');
        if($lists){
            foreach($lists as $vo){
                $userIds[] = $vo['admin_id'];
                if(in_array($vo['group_id'],[2,3])){
                    self::getCustomerSups($vo['admin_id'],$userIds);
                }
            }
        }
        return $userIds;
    }
    /*验证邮箱是否唯一*/
    public static function validEmail($email){
        $user = self::where(['email'=>$email])->count();
        if($user){
            return true;
        }
        /*请求ucenter中心，看看有没有*/
        $ucenter = new \clt\Ucenter(); 
        $uid = $ucenter->getUid($email);
        return intval($uid) > 0 ? true : false;
    }
    /*获取下级的id*/
    public static function getSupUsers($user_id){
        $lists = db('Users')->where('sup_id',$user_id)->field('admin_id')->select();
        $ids = [];
        if(is_array($lists) && $lists){
            foreach($lists as $vo){
                $ids[] = $vo['admin_id'];
            }
        }
        return $ids;
    }
    /*获取管理层用户*/
    public static function getUserLists($user_id=null){
        if(self::isAdmin($user_id)){
            $lists = [];
       
        }elseif(self::isSalesman($user_id)){
            $lists = db('Users')->alias('u')
                ->join('auth_group ag','u.group_id = ag.group_id','left')
                ->field('u.*,ag.title')
                ->where('u.group_id','=','1')->select();
         
        }elseif(self::isCustomer($user_id)){
            $lists = db('Users')->alias('u')
                ->join('auth_group ag','u.group_id = ag.group_id','left')
                ->field('u.*,ag.title')
                ->where('u.group_id','in','2,3')->select();
         
        }
        elseif(self::isChannel($user_id)){
            $lists = db('Users')->alias('u')
                ->join('auth_group ag','u.group_id = ag.group_id','left')
                ->field('u.*,ag.title')
                ->where('u.group_id','=','2')->select();
         
        }
        return $lists;
    }
}
